//
//  ViewController.m
//  蓝牙
//
//  Created by 美食家 on 16/7/15.
//  Copyright © 2016年 不孤独的美食家. All rights reserved.
//

#import "ViewController.h"
#import <GameKit/GameKit.h>
@interface ViewController ()<GKPeerPickerControllerDelegate,UINavigationControllerDelegate,UIImagePickerControllerDelegate>
/**
 *  蓝牙当前会话
 */
@property (nonatomic, strong) GKSession *session;
@property (weak, nonatomic) IBOutlet UIImageView *showImg;
@end

@implementation ViewController

- (void)viewDidLoad {
    [super viewDidLoad];
   
}
#pragma mark - GKPeerPickerControllerDelegate
/* 
 * 蓝牙连接类型
 * Notifies delegate that a connection type was chosen by the user.
 * 连接类型GKPeerPickerConnectionTypeOnline = 1 << 0,
	      GKPeerPickerConnectionTypeNearby = 1 << 1
 */
- (void)peerPickerController:(GKPeerPickerController *)picker didSelectConnectionType:(GKPeerPickerConnectionType)type
{

}
/* 
 * 蓝牙会话类型
 * Notifies delegate that the connection type is requesting a GKSession object.
 * You should return a valid GKSession object for use by the picker. If this method is not implemented or returns 'nil', a default GKSession is created on the delegate's behalf.
 */
#pragma clang diagnostic push
#pragma clang diagnostic ignored "-Wdeprecated-declarations"
- (GKSession *)peerPickerController:(GKPeerPickerController *)picker sessionForConnectionType:(GKPeerPickerConnectionType)type
{
    return  nil;
}
/* 
 * 蓝牙连接已经连接
 * Notifies delegate that the peer was connected to a GKSession.
 */
- (void)peerPickerController:(GKPeerPickerController *)picker didConnectPeer:(NSString *)peerID toSession:(GKSession *)session
{
    // 隐藏选择器
    [picker dismiss];
    // 接收数据的回调  GameKIt  必须实现的
    [session setDataReceiveHandler:self withContext:nil];
    // 保存会话
    self.session = session;
}
#pragma clang diagnostic pop
/* 
 * 用户取消蓝牙连接
 * Notifies delegate that the user cancelled the picker.
 */
- (void)peerPickerControllerDidCancel:(GKPeerPickerController *)picker
{

}
#pragma mark - 蓝牙接收数据的回调方法
// 只要有数据回来  那么就会调用
- (void) receiveData:(NSData *)data fromPeer:(NSString *)peer inSession: (GKSession *)session context:(void *)context
{
    if (!data) return;
    // 转换图片
    UIImage *img = [UIImage imageWithData:data];
    dispatch_async(dispatch_get_main_queue(), ^{
        self.showImg.image = img;
    });
}
#pragma mark - 按钮点击方法
- (IBAction)lookforBlueTeeth:(id)sender {
    // 创建蓝牙选择器
    GKPeerPickerController *picker = [[GKPeerPickerController alloc]init];
    picker.delegate = self;
    // 显示
    [picker show];
}

- (IBAction)selectPicture:(id)sender {
    UIImagePickerController *imgPicker = [[UIImagePickerController alloc]init];
    imgPicker.delegate = self;
    imgPicker.sourceType = UIImagePickerControllerSourceTypeSavedPhotosAlbum;
    [self presentViewController:imgPicker animated:YES completion:nil];
}

- (IBAction)sendPicture:(id)sender {
    if (!self.showImg.image) return;
    // 图片转化
    NSData *data = UIImagePNGRepresentation(self.showImg.image);
    // GKSendDataReliable  安全模式
    // GKSendDataUnreliable 不安全模式
    [self.session sendDataToAllPeers:data withDataMode:GKSendDataUnreliable error:nil];
}
@end
